package com.ddyx.rest;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ddyx.util.ImageUtils;
import com.ddyx.util.PropertiesUtil;
import com.ddyx.util.QRCodeUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.service.IImgUploadService;
import com.ddyx.service.IToolService;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * @ClassName:  ToolController   
 * @Description:通用工具类   
 * @author: liuhoujie
 * @date:   2017年12月21日 下午4:49:50     
 * @Copyright: 2017 dingdian.parllay.cn Inc. All rights reserved. 
 * 注意：本内容仅限于叮当一下(北京)信息技术有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Controller
@RequestMapping("/tool")
public class ToolController {
	
	@Resource
	IImgUploadService service;
	
	@Resource
	IToolService toolService;
	
	/**
	 * @Title: getErweimaImg   
	 * @Description: 普通的二维码生成    带logo的
	 * @param: @param req
	 * @param: @param resp      
	 * @return: void
	 */
//	@SuppressWarnings({ "static-access", "restriction" })
//	@RequestMapping(value="getQRCode_test",method=RequestMethod.POST)
//	public void  getErweimaImg(HttpServletRequest req,HttpServletResponse resp){  
//        String url = req.getParameter("url");  
//        QRCodeUtil handler = new QRCodeUtil();  
//		try {
//			//1.获取图片
//			String urlstr="http://dingdian.parllay.cn/resource/B02S0002/8/8.jpg";
//			String savePath=PropertiesUtil.getProperty("QRCode.logo");
//			String imageName =  "logo1.jpg";
//	        ImageUtils.getImage(urlstr,savePath,imageName);
//			
//			BufferedImage image = handler.createImage(url, savePath+imageName, true); 
//	        //2.将图片对象赋值给imageStream属性  
//	        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
//	        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);   
//            encoder.encode(image);//将image压缩成jpeg格式  
//            byte[] bytes = bos.toByteArray();  
//            
//             //上面都是生成图片，SpringMvc中如何将图片展现，主要是以下  
//             OutputStream stream = resp.getOutputStream();  
//             stream.write(bytes);  
//             stream.flush();  
//             stream.close();  
//        }catch(Exception e){  
//            e.printStackTrace();  
//        }  
//    }  
	
	/**
	 * @Title: getQRCode_wxProgram   
	 * @Description: 生成微信小程序的二维码   
	 * @param: @param req
	 * @param: @param resp      
	 * @return: void
	 */
	@ResponseBody
	@RequestMapping(value="getQRCode_wxProgram",method=RequestMethod.POST)
	public ResponseForm getQRCode_wxProgram(@RequestBody RequestForm requestForm) {
		return toolService.getQRCode_wxProgram(requestForm);
	}
	
	@ResponseBody
	@RequestMapping(value="getAddGoods",method=RequestMethod.POST)
	public ResponseForm getAddGoods(@RequestBody RequestForm requestForm) {
		return toolService.getAddGoods(requestForm);
	}
	
	
	@ResponseBody
	@RequestMapping(value = "getWxToken",method = RequestMethod.POST)
	public ResponseForm getWxToken(HttpServletRequest req) {
		if(req.getHeader("key").equals( PropertiesUtil.getProperty("sys.appid"))) {
			return toolService.getWxToken();
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping(value = "wxPicUpload",method = RequestMethod.POST)
	public ResponseForm wxPicUpload(@RequestBody RequestForm param) {
		return toolService.wxPicUpload(param);
	}
}
